<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户添加</title>
    <meta name="keywords" content="后台用户添加">
    <meta name="description" content="后台用户添加">
    <link rel="shortcut icon" href="favicon.ico">
    <link href="/Public/Admin/css/bootstrap.min.css" rel="stylesheet">
    <link href="/Public/Admin/css/font-awesome.min.css" rel="stylesheet">
    <link href="/Public/Admin/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="/Public/Admin/css/animate.css" rel="stylesheet">
    <link href="/Public/Admin/js/plugins/layer/skin/default/layer.css" rel="stylesheet">
    <link href="/Public/Admin/css/style.css" rel="stylesheet">
    <link href="/Public/Admin/css/plugins/switchery/switchery.css" rel="stylesheet">
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>添加管理用户</h5>
                </div>
                <div class="ibox-content">
                    <form method="post" id="formadd" name="formadd" class="form-horizontal" action="<?php echo U('User/addsave');?>">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">管理员名称：</label>

                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="username" name="username" id="username">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">管理员分组：</label>
                            <div class="col-sm-10">

                                <select class="form-control" name="pid" id="pid">
                                    <option value="">====请选择分组====</option>
                                    <?php if(is_array($opt)): $i = 0; $__LIST__ = $opt;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$vo): $mod = ($i % 2 );++$i;?><option value="<?php echo ($vo["id"]); ?>"><?php echo ($vo["title"]); ?></option><?php endforeach; endif; else: echo "" ;endif; ?> 
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">上传头像：</label>
                            <div class="col-sm-10">
                                <input type="file" name="img"/>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">登录密码：</label>
                            <div class="col-sm-10">
                            <input type="password" class="form-control" name="password" id="password">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">真实姓名：</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="realname" id="realname">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态：</label>

                            <div class="col-sm-10">
                                <input type="checkbox" name="status" id="status" value="1" class="js-switch" checked> 正常
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button class="btn btn-primary" type="submit">保存内容</button>
                                <a class="btn btn-white" href="javascript:history.go(-1)">取消</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/Public/Admin/js/jquery.min.js"></script>
<script src="/Public/Admin/js/bootstrap.min.js"></script>
<script src="/Public/Admin/js/plugins/switchery/switchery.js"></script>
<script src="/Public/Admin/js/plugins/validate/jquery.validate.min.js"></script>
<script src="/Public/Admin/js/plugins/validate/messages_zh.min.js"></script>
<script src="/Public/Admin/js/plugins/layer/layer.js"></script>
<script src="/Public/Admin/js/comment.js"></script>
<script>
    $(function(){
        //匹配用户名和密码的规则
        $.validator.addMethod("checkname",function(value,element,params){
            if(/^[a-zA-Z0-9_-]{3,16}$/.test(value)){
                return true;
            }else{
                return false;
            }
        },"");

        $('#formadd').validate({
            //相当于在input中添加
            //<input type="text" class="form-control" id="username" name="username" id="username" required>
            //前台提交验证表单
            rules:{
                username:"required", 
                password:{required:true,minlength:6,maxlength:15,checkname:true},
                realname:"required",
            },
            messages:{
                username:'用户名不能为空!',
                password:{
                    required:'密码不能为空!',
                    minlength:'密码长度不符合规则',
                    maxlength:'密码长度不符合规则',
                    checkname:'密码不合法'
                },
                realname:"真实姓名不能为空！"
            },
            //提交成功后执行
            submitHandler:function(form){
                var forma = new  FormData(document.getElementById('formadd'));
                console.log(forma);
                $.ajax({
                    url:$(form).attr('action'),//地址是通过form 的action
                    type:'post',
                    dataType:'json',
                    data:forma,//传的对象
                    cache: false,
                    processData: false,
                    contentType: false,
                    success:function(data){
                        if(data.status < 1){
                            comment.error(data.mess);
                        }else{
                            comment.success(data.mess,"<?php echo U('User/index');?>");
                        }
                    }
                });

            }
        });
    });
    var elem = document.querySelector('.js-switch');
    var init = new Switchery(elem,{ size: 'small', jackColor: '#9decff' });  
</script>
</body>
</html>